﻿using System.Collections.Generic;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Clothes.Masters.ModuleData;
using ApplicationLibrary.CounterManager;
using Clothes.Masters.Library;

namespace Clothes.Masters.Forms
{
    public partial class Masters : ApplicationLibrary.DBManager.DocumentForm
    {
        private EnumsManager<DocType> eDocTypeManager = new EnumsManager<DocType>(Properties.Resources.ResourceManager);

        public TabControl MasterTabControl { get { return tbcMaster; } }
        public CounterControl Customer { get { return fncID; } }

        CheckDuplicateTaxNo CheckFiscalTaxCode;

        public Masters()
            : base("Masters")
        {
            InitializeComponent();
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarMastersParam(fncID.CounterNumericValue);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerMasters("mastersForm", new RadarMasters(), this);
            dbManager.AddMaster(this, CL_Masters.Name, true);

            fncID.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.CN_Masters), GlobalInfo.CurrentDate, dbManager.Transaction);


            eDocTypeManager.AttachTo(cbbDocType);

            CheckFiscalTaxCode = new CheckDuplicateTaxNo(dbManager.DBConnection);
        }

        protected override void OnBindData()
        {
            BindControl(fncID, CL_Masters.ID, NullValue.NotSet, Findable.YES);
            BindControl(txtDescription, CL_Masters.Description, Findable.YES);
            BindControl(txtAddress, CL_Masters.Address, Findable.YES);
            BindControl(txtCity, CL_Masters.City);
            BindControl(txtZipCode, CL_Masters.ZipCode);
            BindControl(txtCounty, CL_Masters.County);
            BindControl(txtPhone, CL_Masters.Phone1, Findable.YES);
            BindControl(txtCell, CL_Masters.Phone2, Findable.YES);
            BindControl(txtRefer1, CL_Masters.Refer1);
            BindControl(txtRefer2, CL_Masters.Refer2);
            BindControl(txtFax, CL_Masters.Fax);
            BindControl(txtMail, CL_Masters.Email, Findable.YES);
            BindControl(mtbFiscalCode, CL_Masters.FiscalCode);
            BindControl(lblSurname, CL_Masters.Description);

            BindControl(ckbNotify, CL_Masters.Notify, "Checked");
            BindControl(ckbWarrant, CL_Masters.WarrantSale, "Checked");
            BindControl(cbbDocType, CL_Masters.DocType, "SelectedValue", NullValue.NotSet);
            BindControl(txtDocNo, CL_Masters.DocNo, Findable.YES);

            BindCounter(fncID);
        }

        protected override bool OnAfterAddNew()
        {
            return base.OnAfterAddNew();
        }

        protected override void FocusOnNew()
        {
            txtDescription.Focus();
        }

        #endregion

        private void mtbFiscalCode_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !CheckFiscalTaxCode.CheckFiscalNo(fncID.CounterNumericValue, mtbFiscalCode.Text);
        }

    }

    #region DbManager dbmanagerMasters

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerMasters : DBManager
    {
        public dbmanagerMasters(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new CL_Masters();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(CL_Masters.ID, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam1 = new SqlABParameter("@p1", CL_Masters.ID) { Value = CL_Masters.ID.DefaultValue };
            PList.Add(sParam1);
            return PList;
        }

        protected override void SetParameters(ApplicationLibrary.ExtendedControls.IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }

    #endregion

}
